package com.lero.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lero.dao.DormManagerDao;
import com.lero.model.DormManager;
import com.lero.model.PageBean;
import com.lero.util.DbUtil;
import com.lero.util.ExportExcel;
import com.lero.util.PropertiesUtil;
import com.lero.util.StringUtil;

public class DormManagerServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	DbUtil dbUtil = new DbUtil();
	DormManagerDao dormManagerDao = new DormManagerDao();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		String s_dormManagerText = request.getParameter("s_dormManagerText");
		String searchType = request.getParameter("searchType");
		String page = request.getParameter("page");
		String action = request.getParameter("action");
		DormManager dormManager = new DormManager();
		if ("preSave".equals(action)) {
			dormManagerPreSave(request, response);
			return;
		} else if ("save".equals(action)) {
			dormManagerSave(request, response);
			return;
		} else if("export".equals(action)){
			dormManagerExportExcel(request, response);
			return;
		} else if("delete".equals(action)){
			dormManagerDelete(request, response);
			return;
		} else if ("list".equals(action)) {
			if (StringUtil.isNotEmpty(s_dormManagerText)) {
				if ("name".equals(searchType)) {
					dormManager.setName(s_dormManagerText);
				} else if ("userName".equals(searchType)) {
					dormManager.setUserName(s_dormManagerText);
				}
			}
			session.removeAttribute("s_dormManagerText");
			session.removeAttribute("searchType");
			request.setAttribute("s_dormManagerText", s_dormManagerText);
			request.setAttribute("searchType", searchType);
		} else if ("search".equals(action)) {
			if (StringUtil.isNotEmpty(s_dormManagerText)) {
				if ("name".equals(searchType)) {
					dormManager.setName(s_dormManagerText);
				} else if ("userName".equals(searchType)) {
					dormManager.setUserName(s_dormManagerText);
				}
				session.setAttribute("searchType", searchType);
				session.setAttribute("s_dormManagerText", s_dormManagerText);
			} else {
				session.removeAttribute("s_dormManagerText");
				session.removeAttribute("searchType");
			}
		} else {
			if (StringUtil.isNotEmpty(s_dormManagerText)) {
				if ("name".equals(searchType)) {
					dormManager.setName(s_dormManagerText);
				} else if ("userName".equals(searchType)) {
					dormManager.setUserName(s_dormManagerText);
				}
				session.setAttribute("searchType", searchType);
				session.setAttribute("s_dormManagerText", s_dormManagerText);
			}
			if (StringUtil.isEmpty(s_dormManagerText)) {
				Object o1 = session.getAttribute("s_dormManagerText");
				Object o2 = session.getAttribute("searchType");
				if (o1 != null) {
					if ("name".equals(o2)) {
						dormManager.setName((String) o1);
					} else if ("userName".equals(o2)) {
						dormManager.setUserName((String) o1);
					}
				}
			}
		}
		if (StringUtil.isEmpty(page)) {
			page = "1";
		}
		Connection con = null;
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer
				.parseInt(PropertiesUtil.getValue("pageSize")));
		request.setAttribute("pageSize", pageBean.getPageSize());
		request.setAttribute("page", pageBean.getPage());
		try {
			con = dbUtil.getCon();
			List<DormManager> dormManagerList = dormManagerDao.dormManagerList(
					con, pageBean, dormManager);
			int total = dormManagerDao.dormManagerCount(con, dormManager);
			String pageCode = this.genPagation(total, Integer.parseInt(page),
					Integer.parseInt(PropertiesUtil.getValue("pageSize")));
			request.setAttribute("pageCode", pageCode);
			request.setAttribute("dormManagerList", dormManagerList);
			request.setAttribute("mainPage", "admin/dormManager.jsp");
			request.getRequestDispatcher("mainAdmin.jsp").forward(request,
					response);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void dormManagerDelete(HttpServletRequest request,
			HttpServletResponse response) {
		String dormManagerId = request.getParameter("dormManagerId");
		Connection con = null;
		try {
			con = dbUtil.getCon();
			dormManagerDao.dormManagerDelete(con, dormManagerId);
			request.getRequestDispatcher("dormManager?action=list").forward(
					request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void dormManagerSave(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String dormManagerId = request.getParameter("dormManagerId");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String tel = request.getParameter("tel");
		DormManager dormManager = new DormManager(userName, password, name,
				sex, tel);
		if (StringUtil.isNotEmpty(dormManagerId)) {
			dormManager.setDormManagerId(Integer.parseInt(dormManagerId));
		}
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int saveNum = 0;
			if (StringUtil.isNotEmpty(dormManagerId)) {
				saveNum = dormManagerDao.dormManagerUpdate(con, dormManager);
			} else if (dormManagerDao.haveManagerByUser(con, dormManager
					.getUserName())) {
				request.setAttribute("dormManager", dormManager);
				request.setAttribute("error", "该用户名已存在");
				request.setAttribute("mainPage", "admin/dormManagerSave.jsp");
				request.getRequestDispatcher("mainAdmin.jsp").forward(request,
						response);
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return;
			} else {
				saveNum = dormManagerDao.dormManagerAdd(con, dormManager);
			}
			if (saveNum > 0) {
				request.getRequestDispatcher("dormManager?action=list")
						.forward(request, response);
			} else {
				request.setAttribute("dormManager", dormManager);
				request.setAttribute("error", "保存失败");
				request.setAttribute("mainPage",
						"dormManager/dormManagerSave.jsp");
				request.getRequestDispatcher("mainAdmin.jsp").forward(request,
						response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void dormManagerPreSave(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String dormManagerId = request.getParameter("dormManagerId");
		if (StringUtil.isNotEmpty(dormManagerId)) {
			Connection con = null;
			try {
				con = dbUtil.getCon();
				DormManager dormManager = dormManagerDao.dormManagerShow(con,
						dormManagerId);
				request.setAttribute("dormManager", dormManager);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		request.setAttribute("mainPage", "admin/dormManagerSave.jsp");
		request.getRequestDispatcher("mainAdmin.jsp")
				.forward(request, response);
	}

	private String genPagation(int totalNum, int currentPage, int pageSize) {
		int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize
				: totalNum / pageSize + 1;
		StringBuffer pageCode = new StringBuffer();
		pageCode.append("<li><a href='dormManager?page=1'>首页</a></li>");
		if (currentPage == 1) {
			pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
		} else {
			pageCode.append("<li><a href='dormManager?page="
					+ (currentPage - 1) + "'>上一页</a></li>");
		}
		for (int i = currentPage - 2; i <= currentPage + 2; i++) {
			if (i < 1 || i > totalPage) {
				continue;
			}
			if (i == currentPage) {
				pageCode.append("<li class='active'><a href='#'>" + i
						+ "</a></li>");
			} else {
				pageCode.append("<li><a href='dormManager?page=" + i + "'>" + i
						+ "</a></li>");
			}
		}
		if (currentPage == totalPage) {
			pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
		} else {
			pageCode.append("<li><a href='dormManager?page="
					+ (currentPage + 1) + "'>下一页</a></li>");
		}
		pageCode.append("<li><a href='dormManager?page=" + totalPage
				+ "'>尾页</a></li>");
		return pageCode.toString();
	}
	
	/**
	 * 导出excel 
	 * @author jiziyang
	 */
	private void dormManagerExportExcel(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException {
		DormManager dormManager = new DormManager();
		String page = request.getParameter("page");
		List<DormManager> dormManagerList = new ArrayList<DormManager>();
		if(StringUtil.isEmpty(page)) {
			page="1";
		}
		Connection con = null;
		try {
			con=dbUtil.getCon();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		try {
			dormManagerList = dormManagerDao.dormManagerExportList(con, dormManager);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		List<HashMap<String, Object>> listMap = new ArrayList<HashMap<String,Object>>();
        
        for(int i=0;i<dormManagerList.size();i++){
        	HashMap<String,Object> dataMap = new HashMap<String, Object>();
            dataMap.put("dormManagerId", dormManagerList.get(i).getDormManagerId());
            dataMap.put("name", dormManagerList.get(i).getName());
            dataMap.put("sex", dormManagerList.get(i).getSex());
            dataMap.put("tel", dormManagerList.get(i).getTel());
            if (null == dormManagerList.get(i).getDormBuildName() || "".equals(dormManagerList.get(i).getDormBuildName())) {
            	dataMap.put("dormBuildName","无");
			}else {
				dataMap.put("dormBuildName", dormManagerList.get(i).getDormBuildName());
			}
            dataMap.put("userName", dormManagerList.get(i).getUserName());
            listMap.add(dataMap);
        }
        String title = "宿舍管理员名单";
        String[] rowsName = new String[]{"编号","姓名","性别","电话","宿舍楼","用户名"};
        List<Object[]>  dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < listMap.size(); i++) {
            HashMap<String, Object> data = listMap.get(i);
            objs = new Object[rowsName.length];
            objs[0] = data.get("dormManagerId");
            objs[1] = data.get("name");
            objs[2] = data.get("sex");
            objs[3] = data.get("tel");
            objs[4] = data.get("dormBuildName");
            objs[5] = data.get("userName");
            dataList.add(objs);
            
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList,"宿舍管理员");
        try {
			ex.export();
			request.getRequestDispatcher("dormManager?action=list").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
